package cc.mrbird.febs.accounting.controller;

import cc.mrbird.febs.accounting.Constant.CompanyConstant;
import cc.mrbird.febs.accounting.entity.DetailCost;
import cc.mrbird.febs.accounting.dto.detailCost.DetailCostParam;
import cc.mrbird.febs.accounting.entity.StandardMain;
import cc.mrbird.febs.accounting.service.IMainCostService;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.utils.FebsUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 *  Controller
 *
 * @author hbh
 * @date 2022-02-17 23:27:01
 */
@Slf4j
@Validated
@Controller
public class MainCostController extends BaseController {

    @Resource
    private IMainCostService mainCostService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "mainCost")
    public String mainCostIndex(){
        return FebsUtil.view("mainCost/mainCost");
    }

    /**
     * @author HBH
     * @description 不同公司调用不同业务层
     * @date 2022/3/4
     */
    @GetMapping("mainCost/queryMainCost")
    @ResponseBody
    public FebsResponse queryMainCost(String yearMonth) {
        if (yearMonth.indexOf("-") < 3) {
            return new FebsResponse().fail().message("传入的参数有误");
        }
        List<StandardMain> data = new ArrayList<>();
        if (CompanyConstant.companyType == 1 || CompanyConstant.companyType == 2) {
            data = this.mainCostService.queryMainCost(yearMonth);
        }
        return new FebsResponse().success().data(data);
    }
    
    /**
     * @author HBH
     * @description 不同公司调用不同业务层
     * @date 2022/3/4
     */
    @PostMapping("mainCost/save")
    @ResponseBody
    @RequiresPermissions("mainCost:save")
    public FebsResponse saveDetailCost(@ModelAttribute DetailCostParam detailCostParam) {
        if (Objects.isNull(detailCostParam) || Objects.isNull(detailCostParam.getYearMonth())) {
            return new FebsResponse().fail().message("参数不正确");
        }
        if (StringUtils.isNotBlank(detailCostParam.getDetailCostListStr())) {
            List<DetailCost> detailCosts = JSONObject.parseArray(detailCostParam.getDetailCostListStr(), DetailCost.class);
            List<DetailCost> collect = detailCosts.stream().filter(e -> Objects.nonNull(e.getCost())).collect(Collectors.toList());
            detailCostParam.setDetailCostList(collect);
        }
        if (CompanyConstant.companyType == 1 || CompanyConstant.companyType == 2) {
            mainCostService.saveDetailCost(detailCostParam);
        }
        return new FebsResponse().success();
    }
}
